﻿import {ActionContext} from 'vuex';
import ListModule from '@/store/modules/list-module';
import ListState from '@/store/modules/list-state';
import PageResult from '@/store/entities/page-result';
import Ajax from '@/lib/ajax';
import Book from '@/store/entities/book/book';

interface BookState extends ListState<Book>{
    bookForEdit:any,
    excelFile:any,
    book:any,
}

class BookModule extends ListModule<BookState,any,Book>{
    state={
        totalCount: 0,
        currentPage: 1,
        pageSize: 10,
        list: new Array<Book>(),
        loading: false,
        bookForEdit:{},
        excelFile:{},
        book:{},
    };
    actions={
        async obtainBooks(context:ActionContext<BookState, any>,payload:any){
            context.state.loading=true;
            const response=await Ajax.post('/api/services/app/Book/ObtainBooks',payload.data);
            context.state.loading=false;
            const result=response.data.result as PageResult<Book>;
            context.state.totalCount=result.totalCount;
            context.state.list=result.items;
        },
        async createOrUpdateBook(context:ActionContext<BookState, any>,payload:any){
            await Ajax.post('/api/services/app/Book/CreateOrUpdateBook',payload.data);
        },
        async deleteBook(context:ActionContext<BookState, any>,payload:any){
            await Ajax.delete('/api/services/app/Book/DeleteBook?Id='+payload.data.id);
        },
        async getBookForEdit(context:ActionContext<BookState, any>,payload:any){
            const response=await Ajax.get('/api/services/app/Book/GetBookForEdit?Id='+payload.data.id);
            context.state.bookForEdit=response.data.result;
        },
        async getBook(context:ActionContext<BookState, any>,payload:any){
            const response=await Ajax.get('/api/services/app/Book/GetBook?Id='+payload.data.id);
            context.state.book=response.data.result;
        },
        async obtainBooksToExcel(context:ActionContext<BookState, any>,payload:any){
            const response=await Ajax.post('/api/services/app/Book/ObtainBooksToExcel',payload.data);
            context.state.excelFile=response.data.result;
        },
    };
    mutations = {
        setCurrentPage(state: BookState, page: number) {
            state.currentPage = page;
        },
        setPageSize(state: BookState, pageSize: number) {
            state.pageSize = pageSize;
        },
    };
}
const bookModule=new BookModule();
export default bookModule;